package com.lhz.collector.service;

import com.lhz.collector.entity.ParseRule;
import com.lhz.collector.mapper.ParseRuleMapper;
import com.lhz.collector.parse.lexer.BaseLexer;
import com.lhz.collector.parse.util.ParseJsonRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by LHZ on 2017/4/13.
 */
@Service
public class ParseServiceImpl implements ParseService {
    @Autowired
    private ParseRuleMapper parseRuleMapper;
    @Override
    public List<ParseRule> findAllList() {
        return parseRuleMapper.findList(new ParseRule());
    }

    @Override
    public ParseRule getById(long id) {
        return parseRuleMapper.get(id);
    }

    @Override
    public boolean insert(ParseRule parseRule) {
        return parseRuleMapper.insert(parseRule)>0;
    }

    @Override
    public boolean update(ParseRule parseRule) {
        return parseRuleMapper.update(parseRule)>0;
    }

    @Override
    public boolean delete(ParseRule parseRule) {
        return parseRuleMapper.delete(parseRule)>0;
    }

    @Override
    public Map<String,Object> preview(String jsonRule) {
        ParseJsonRule parseJsonRule=new ParseJsonRule();
        BaseLexer lexer=parseJsonRule.parseRule(jsonRule);
        return lexer.parse(lexer.getSample());
    }
}
